﻿using System.Reflection;
using System.Web.Mvc;
using log4net;

namespace Syufy.BINLookup.Web.Helpers
{
    /// <summary>
    /// Action filter to log error messages with log4net.
    /// </summary>
    public class LogErrorAttribute : ActionFilterAttribute, IExceptionFilter
    {
        #region Logging
        /// <summary>
        /// Access to the log4Net logging object
        /// </summary>
        protected static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        #endregion

        /// <summary>
        /// OnException override.
        /// </summary>
        /// <param name="filterContext"></param>
        public void OnException(ExceptionContext filterContext)
        {
            if (filterContext != null && filterContext.Exception != null)
            {
                string controller = filterContext.RouteData.Values["controller"].ToString();
                string action = filterContext.RouteData.Values["action"].ToString();
                string loggerName = string.Format("{0}Controller.{1}", controller, action);

                log.Error(filterContext.Exception.Message, filterContext.Exception);
            }
        }
    }
}